using Godot;
using System;

public partial class enemy : CharacterBody2D
{
	// 交互关系放置在第五个图层
	Random random = new Random();
	private Area2D EnemyArea;
	private Vector2 Player;
	private Timer outTime;
	private bool PlayerScan = false;
	private float Speed = 100.0f;
	private Vector2 SpeedVector;
	private Vector2 moveSpeedVector = new Vector2(40.0f, 0.0f);
	private int PositionLength = 10;
	private bool timeout;
	private Vector2 inPosition;

	public override void _Ready()
	{
		EnemyArea = GetNode<Area2D>("enemy");
		outTime = GetNode<Timer>("outTime");
		inPosition = Position;
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{	
		if (PlayerScan)
		{
			SpeedVector = Position.DirectionTo(Player);
			MoveAndCollide(new Vector2(SpeedVector.X * (float)(Speed * delta), SpeedVector.Y * (float)(Speed * delta)));
			
			if (timeout)
			{
				PlayerScan = false;
			}
		}
		else
		{
			moveSpeedVector = moveSpeedVector.Rotated(0.01f);
			MoveAndCollide(new Vector2((float)(moveSpeedVector.X * delta), (float)(moveSpeedVector.Y * delta)));
			Position += new Vector2();
			MoveAndSlide();
		}
	}


	private void _on_enemy_area_entered(Area2D area)
	{
		PlayerScan = true;
		timeout = false;
		outTime.Start();
		Player = area.Position;
	}

	private void _on_out_time_timeout()
	{
		timeout = true;
	}
}
